package cn.xu.tool.big.a.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * 类名: FileUtils
 * 描述: TODO
 * 作者: ClarkXu clark2021@qq.com
 * 日期: 2025/7/8
 */
public class FileUtil {

    public static void write(String content, String filePath) throws IOException {
        Path path = Paths.get(filePath);
        byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
        Files.write(path, bytes);
    }
    public static void writeAppend(String content, String filePath) throws IOException {
        Path path = Paths.get(filePath);
        Path parentDir = path.getParent();
        if (parentDir != null) {
            Files.createDirectories(parentDir);
        }
        String contentWithNewLine = content + System.lineSeparator();
        byte[] bytes = contentWithNewLine.getBytes(StandardCharsets.UTF_8);
        Files.write(
                path,
                bytes,
                StandardOpenOption.APPEND,
                StandardOpenOption.CREATE
        );
    }

    public static void main(String[] args) throws IOException {
        writeAppend("11111", "D:\\work\\code\\clark\\gitee\\big_a\\datas\\kline_merge\\cn_002468\\test.json");
        writeAppend("22211111", "D:\\work\\code\\clark\\gitee\\big_a\\datas\\kline_merge\\cn_002468\\test.json");
    }

    public static String read(String filePath) throws IOException {
        Path path = Paths.get(filePath);
        byte[] bytes = Files.readAllBytes(path);
        String content = new String(bytes, StandardCharsets.UTF_8);
        return content;
    }
}
